package cn.edu.zjicm.wordsnet_d.activity;

import com.umeng.analytics.MobclickAgent;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.DataValidator;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.EditText;
import android.widget.Toast;

public class ModifyPasswordActivity extends WordBaseActivity implements OnClickListener,OnFocusChangeListener{
	
	public static void startActivity(Context context) {
		Intent i = new Intent(context, ModifyPasswordActivity.class);
		context.startActivity(i);
	}
	
	EditText oldpassword, password, password2;
	Button register;
	ImageView old_pwd_clear,pwd_clear,pwd2_clear;
	InputMethodManager imm;

	Handler registerHandler = new Handler() {
		ProgressDialog dialog2 = null;
		
		public void handleMessage(android.os.Message msg) {
			if(msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}
			
			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(ModifyPasswordActivity.this, null, "正在修改密码...");	
				break;
			case UserInterface.MSG_SUCCESS:
				Toast.makeText(ModifyPasswordActivity.this, "修改密码成功",	Toast.LENGTH_SHORT).show();
				finish();
				break;			
			case UserInterface.MSG_WRONG_PASSWORD:
				Toast.makeText(ModifyPasswordActivity.this, "原密码输入错误", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_BAD_NETWORK:
				Toast.makeText(ModifyPasswordActivity.this, UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(ModifyPasswordActivity.this, UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("修改密码");
		setContentView(R.layout.activity_modify_password);
		
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

		oldpassword = (EditText) findViewById(R.id.old_pwd);
		oldpassword.setOnFocusChangeListener(this);
		
		password = (EditText) findViewById(R.id.register_pwd);
		password.setOnFocusChangeListener(this);
		
		password2 = (EditText) findViewById(R.id.register_pwd2);
		password2.setOnFocusChangeListener(this);
		register = (Button) findViewById(R.id.register_btn);
		register.setOnClickListener(this);
		
		old_pwd_clear = (ImageView) findViewById(R.id.old_pwd_clear);
		pwd_clear = (ImageView) findViewById(R.id.register_pwd_clear);
		pwd2_clear = (ImageView) findViewById(R.id.register_pwd2_clear);
		
		old_pwd_clear.setOnClickListener(this);
		pwd_clear.setOnClickListener(this);
		pwd2_clear.setOnClickListener(this);
		
		oldpassword.addTextChangedListener(textWatcher);
		password.addTextChangedListener(textWatcher);
		password2.addTextChangedListener(textWatcher);
				
		//mMask = findViewById(R.id.popupwindow_mask);
	}
	
	//private View mMask; // 弹出下载窗口时的遮罩
	
	protected void toModifyPassword() {
		// TODO Auto-generated method stub
		registerHandler.sendMessage(registerHandler.obtainMessage(UserInterface.MSG_START));
		UserInterface.getInstance(ModifyPasswordActivity.this).modifyPassword(
				oldpassword.getText().toString(),
				password.getText().toString(), registerHandler);
	}
	
	void checkRegisterInfo(){
		boolean flag = true;
		if (oldpassword.hasFocus()) {
			if (DataValidator.EditIsNull(oldpassword.getText().toString())) {
				flag = false;
				old_pwd_clear.setVisibility(View.GONE);
			} else {
				old_pwd_clear.setVisibility(View.VISIBLE);
			}
			if (DataValidator.EditIsNull(password.getText().toString()) || DataValidator.EditIsNull(password2.getText().toString())) {
				flag = false;
			}
		}
		if (password.hasFocus()) {
			if (DataValidator.EditIsNull(password.getText().toString())) {
				flag = false;
				pwd_clear.setVisibility(View.GONE);
			} else {
				pwd_clear.setVisibility(View.VISIBLE);
			}
			if (DataValidator.EditIsNull(oldpassword.getText().toString()) || DataValidator.EditIsNull(password2.getText().toString())) {
				flag = false;
			}
		}
		if (password2.hasFocus()) {
			if (DataValidator.EditIsNull(password2.getText().toString())) {
				flag = false;
				pwd2_clear.setVisibility(View.GONE);
			} else {
				pwd2_clear.setVisibility(View.VISIBLE);
			}
			if (DataValidator.EditIsNull(password.getText().toString()) || DataValidator.EditIsNull(oldpassword.getText().toString())) {
				flag = false;
			}
		}
		if (!flag) {
			register.setEnabled(false);
		} else {
			register.setEnabled(true);
		}
	}

	TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			checkRegisterInfo();
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.old_pwd_clear:
			oldpassword.setText("");
			old_pwd_clear.setVisibility(View.GONE);
			break;

		case R.id.register_pwd_clear:
			password.setText("");
			pwd_clear.setVisibility(View.GONE);
			break;
		case R.id.register_pwd2_clear:
			password2.setText("");
			pwd2_clear.setVisibility(View.GONE);
			break;
		case R.id.register_btn:
			if (!DataValidator.PwdIsValidate(oldpassword.getText().toString())) {
				Toast.makeText(ModifyPasswordActivity.this, "原密码格式错误", Toast.LENGTH_SHORT).show();
			} else if (!DataValidator.PwdIsValidate(password.getText()
					.toString())) {
				Toast.makeText(ModifyPasswordActivity.this, "密码格式错误，请输入6~16位数字或字母", Toast.LENGTH_SHORT).show();
			} else if (!DataValidator.PwdIsMatch(password.getText().toString(),
					password2.getText().toString())) {
				Toast.makeText(ModifyPasswordActivity.this, "两次输入密码不同", Toast.LENGTH_SHORT).show();
			} else {
				toModifyPassword();
				imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
			}			
			break;
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.old_pwd:
			if (v.hasFocus() && !DataValidator.EditIsNull(oldpassword.getText().toString())) {
				old_pwd_clear.setVisibility(View.VISIBLE);
			} else {
				old_pwd_clear.setVisibility(View.GONE);
			}
			break;

		case R.id.register_pwd:
			if (v.hasFocus() && !DataValidator.EditIsNull(password.getText().toString())) {
				pwd_clear.setVisibility(View.VISIBLE);
			} else {
				pwd_clear.setVisibility(View.GONE);
			}
			break;
		case R.id.register_pwd2:
			if (v.hasFocus() && !DataValidator.EditIsNull(password2.getText().toString())) {
				pwd2_clear.setVisibility(View.VISIBLE);
			} else {
				pwd2_clear.setVisibility(View.GONE);
			}
			break;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
